#include "mq_connection.hpp"

void callback(castlemq::Channel::ptr& channel, const std::string& ctag,const castlemq::BasicProperties* bp,const std::string& body)
{
    std::cout << ctag << "消费了信息：" << body << std::endl;
    channel->basicAck(bp->id());
}

int main(int argc,char* argv[])
{
    if(argc != 2)
    {
        std::cout<<"usage: ./consume_client queue1"<<std::endl;
        return 0;
    }
    //实例化异步工作线程
    castlemq::AsyncWorker::ptr awp = std::make_shared<castlemq::AsyncWorker>();

    //实例化连接对象
    castlemq::Connection::ptr conn = std::make_shared<castlemq::Connection>("127.0.0.1",8085,awp);

    //通过连接创建信道
    castlemq::Channel::ptr channel = conn->openChannel();

    //通过信道提供的服务完成所需
    //1.声明一个exchange1，广播
    google::protobuf::Map<std::string, std::string> tmp_map;
    channel->declareExchange("exchange1",castlemq::ExchangeType::TOPIC,true,false,tmp_map);
    //2.声明queue1，和queue2
    channel->declareQueue("queue1",true,false,false,tmp_map);
    channel->declareQueue("queue2",true,false,false,tmp_map);
    //3.queue1-exchange1  binding-key为queue1
    channel->queueBind("exchange1","queue1","queue1");
    //4.queue2-exchange1  binding-key为news.music.#
    channel->queueBind("exchange1","queue2","news.music.#");

    auto functor = std::bind(callback,channel,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3);
    channel->basicConsume("consumer1",argv[1],false,functor);

    while(1)
    {
        std::this_thread::sleep_for(std::chrono::seconds(3));
    }

    conn->closeChannel(channel);

    return 0;
}